#include "expmonthview.h"
#include "ui_expmonthview.h"
#include<QDebug>
#include<QMessageBox>
#include<QDesktopWidget>
#include<QtSql/QSqlQuery>

ExpMonthView::ExpMonthView(int id,QString month,int mm,int yy,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ExpMonthView)
{
    ui->setupUi(this);
    QRect in=QApplication::desktop()->availableGeometry(this);
    ui->scrollArea->setGeometry(0,0,(in.width()),(in.height()));
    //ui->listWidget->setGeometry(0,50,(q.width()-10),(q.height()-30));
    ui->listWidget->setEditFocus(true);
    filter_id=id;
    m=month;
    //ui->label_msg->setVisible(false);
    int fontSize=10;
    ui->label_header->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));
    QAction* backAction = new QAction( tr("Back"), this );
    backAction->setSoftKeyRole( QAction::NegativeSoftKey );
    connect(backAction, SIGNAL(triggered()), this, SLOT(back()));
    addAction( backAction );
    if(filter_id==1)
        ui->label_header->setText(tr("Expenses in %1 ").arg(month));
    else
        ui->label_header->setText(tr("Expenses in %1 ").arg(yy));
    if(db.db_connect())
    {
        QSqlQuery q;
        QDate day_one,day_last;
        int count=0;
        q.prepare(QString("select * from expense_table where exp_date >= ? and exp_date <= ?"));
        if(filter_id==1)//For monthly view
        {
            day_one.setDate(yy,mm,1);
            day_last.setDate(yy,mm,day_one.daysInMonth());
        }
        else if(filter_id==2)//For yearly view
        {
            day_one.setDate(yy,1,1);//First day of year
            day_last.setDate(yy,12,31);//Last day of year
        }
        q.addBindValue(day_one);
        q.addBindValue(day_last);
        if(q.exec())
        {
                while(q.next())
                {
                        item1=new QListWidgetItem(QString("%1 : %2").arg(q.value(5).toString()).arg(q.value(1).toDouble()),ui->listWidget,QListWidgetItem::UserType);
                        item1->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));
                        item1->setToolTip(QString("%1").arg(q.value(0).toString()));
                        count++;
                 }
                if(count==0)
             {
                ui->listWidget->hide();
                //ui->label_header->setVisible(false);
               // ui->label_msg->setVisible(true);
                ui->label_header->setText(tr("No expense record found"));
             }
        }
        db.db_close();
    }
}

ExpMonthView::~ExpMonthView()
{
    delete ui;
}
void ExpMonthView::back()
{
    (new ExpMonth(filter_id,1))->show();
}

void ExpMonthView::on_listWidget_itemActivated(QListWidgetItem* item)
{
    if(filter_id==1)
    {
       ExpDetails *e=new ExpDetails(item->toolTip().toInt(),2);
       e->setMonth(m);
       e->show();
    }
    else
    {
        ExpDetails *e=new ExpDetails(item->toolTip().toInt(),3);
        e->setMonth(m);
        e->show();
    }
}
